/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.template.bean.docx;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.opu.vcforms.template.util.Loadable;
import org.opu.vcforms.template.util.SimpleUtil;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

/**
 *
 * @author yura
 */
@Root(name="meta-inf")
public class MetaInf implements Loadable{

    @ElementList(required = false, type = ContentDescription.class, name = "source")
    private ArrayList<ContentDescription> contentDescriptions;

    @ElementList(required = false, type = VarDescription.class, name = "controls")
    private ArrayList<VarDescription> varDescriptions;

    public ArrayList<ContentDescription> getContentDescriptions() {
        return contentDescriptions;
    }

    public void setContentDescriptions(ArrayList<ContentDescription> contentDescriptions) {
        this.contentDescriptions = contentDescriptions;
    }

    public ArrayList<VarDescription> getVarDescriptions() {
        return varDescriptions;
    }

    public void setVarDescriptions(ArrayList<VarDescription> varDescriptions) {
        this.varDescriptions = varDescriptions;
    }

    @Override
    public void load(InputStream inputStream) throws IOException {
        MetaInf read = SimpleUtil.read(inputStream, MetaInf.class);
        setContentDescriptions(read.getContentDescriptions());
        setVarDescriptions(read.getVarDescriptions());
    }

    @Override
    public void store(OutputStream outputStream) throws IOException {
        SimpleUtil.write(outputStream, this);
    }

    /**
     * Return set of tag names where contan must be inserted.
     * @see ContentDescription#getIn()
     * @return set of tag names where contan must be inserted.
     */
    public Collection<String> getContentLevels(){
        ArrayList<ContentDescription> contents = getContentDescriptions();

        if (contents == null || contents.isEmpty()){
            return Collections.emptySet();
        }

        Set<String> set = new HashSet<String>();

        for (ContentDescription content : contents) {
            String in = content.getIn();
            if (in != null && !in.isEmpty()){
                set.add(in);
            }
        }

        return set;
    }
}
